package service

import (
	"RocFamilySystem/tools/container"
	"fmt"
	"log"
	"reflect"
	"runtime"
	"strings"
)

var registerContainer = make(map[string]any)

func InitService() {
	for key, val := range registerContainer {
		repository, _ := container.GetBeanContainer("repository." + strings.ReplaceAll(key, "Service", "Repository"))
		v := reflect.ValueOf(val)
		v.MethodByName("SetRepository").Call([]reflect.Value{reflect.ValueOf(repository)})
		container.PushContainer(val)
	}
}

type PageInfo struct {
	List     any `json:"list"`
	PageNum  int `json:"pageNum"`
	PageSize int `json:"pageSize"`
	Total    int `json:"total"`
}

func toPageInfo(pageNum, pageSize, total int, list any) PageInfo {
	return PageInfo{
		list,
		pageNum + 1,
		pageSize,
		total,
	}
}

func handleServiceErr(err error, msg string) {
	_, file, line, _ := runtime.Caller(1)
	if err != nil {
		fmt.Printf("%s:%d\n", file, line)
		log.Println("Service执行错误：", err.Error())
		log.Panicln("系统错误：", msg+err.Error())
	}
}
